/**
 * Created by ly on 2017/10/12.
 */
var hlArrayUtil = (function() {
  var arrayUtil = {
		//获取隐藏dom的宽高 ('width/height', el 指定的dom)
		getDomWidthOrHeight:function(widthOrHeight, el){
		    var clone=el.cloneNode(true);
		    clone.style.display="block";
		    clone.style.position="fixed";
		　　	clone.style.left='-1000000px';
		　　	clone.style.top='-100000px';
				document.body.appendChild(clone);
		    
		    var width=clone.offsetWidth;
		    var height=clone.offsetHeight;
		    document.body.removeChild(clone);
		    return widthOrHeight=="width"?width:height;    
		},
    /**
     * 替换数组中对象的健
     * @param {array} data
     * @param {array} arr  要替换的对象健  [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }]
     */
		replaceData:function (data, arr) {
      data.map(function(item){
        arr.map(function(item0){
          item[item0.rKey ? item0.rKey : 'code'] = item[item0.key]
        })
      })
			return data
		},
    /**
     * 数组中去除某个对象key value相同的项*
     * @param {array} arr  要切割的数组
     * @param {smArrLen} len  切割若干小数组的长度
     */
    sliceArr: function (array, smArrLen) {
       var newArr = [];
       for (var x = 0; x < Math.ceil(array.length / smArrLen); x++) {
         var start = x * smArrLen;
         var end = start + smArrLen;
         newArr.push(array.slice(start, end));
       }
       return newArr;
    },
    /*数组去重*/
    arrUnique: function(arr) {
      var res = [];
      for (var i = 0, j = arr.length; i < j; i++) {
        if (res.indexOf(arr[i]) === -1) {
          res.push(arr[i]);
        }
      }
      return res;
    },
		//俩个数组根据 参数 field 比较， 找出不同的数组
		diffent: function(fArr,cArr,field){
		    var diffRes = []
		    var fDatas = []
		    var cDatas = []
		    for(var i in fArr){
		        var flg = false
		        for(var j in cArr){
		            if(cArr[j][field]===fArr[i][field]){
		                flg = true
		                break
		            }
		        }
		        if(!flg){
		            fDatas.push(fArr[i])
		        }
		    }
		    for(var i in cArr){
		        var flg = false
		        for(var j in fArr){
		            if(fArr[j][field]===cArr[i][field]){
		                flg = true
		                break
		            }
		        }
		        if(!flg){
		            cDatas.push(cArr[i])
		        }
		    }

				return cDatas.concat(fDatas)
		},
    compareAsc: function (propertyName) {
      return function (object1, object2) {
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if (value2 < value1) {
          return 1;
        } else if (value2 > value1) {
          return -1;
        } else {
          return 0;
        }
      }
    },
    compareAvg:function(property) {
      return function(a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
      }
    },
    /**
     * 数组中去除某个对象key value相同的项*
     * @param {arr} arr  去重的数组
     * @param {string} key  去重的键
     * @return {arr} 返回去重后的新数组
     */
    removeRepeat: function(arr, key) {
      if (!arr.length) return [];
      var res = [arr[0]];
      for (var i = 1; i < arr.length; i++) {
        var repeat = false;
        for (var j = 0; j < res.length; j++) {
          if (arr[i][key] == res[j][key]) {
            repeat = true;
          }
        }
        if (!repeat) {
          res.push(arr[i]);
        }
      }
      return res;
    },
    //传递数组 ---> {name:value, count:count} 返回值和数组出现的次数
    arrZero: function(arr) {
      var newArr = [];
      for (var i = 0; i < arr.length; i++) {
        var temp = arr[i];
        var count = 0;
        for (var j = 0; j < arr.length; j++) {
          if (arr[j] == temp) {
            count++;
            arr[j] = -1;
          }
        }
        if (temp != -1) {
          newArr.push({
            'name': temp,
            'count': count
          })
        }
      }
      return newArr;
    },
    /** 删除数组中指定索引的数据 **/
    deleteIndexItem: function(arr, index) {
      if (index < 0) return arr;
      return arr.slice(0, index).concat(arr.slice(index + 1, arr.length))
    },
    /*删除数组中指定的数据*/
    deleteItem: function(arr, item) {
      if (arr.indexOf(item) < 0) return arr;
      return this.deleteIndexItem(arr, arr.indexOf(item))
    },
    /**
     * 删除数组中指定的多个数据
     * @param {arr || string} items 删除的数据项
     * @return {arr} 返回去重后的新数组
     */
    deleteMultipleItem: function(arr, items) {
      if (typeof items === 'string') items = items.split(',')
      for (var i = 0; i < items.length; i++) {
        arr = this.deleteItem(arr, items[i])
      }
      return arr
    },
    /**
     * 生成1-100之间的随机数
     * @return {number} 返回随机后的新数字
     */
    random: function() {
      return Number((100 * Math.random()).toFixed(2))
    },
    /**
     * 分割数组为二维数组
     * @param  {array} 分割的数组
     * @param  {number}  每组的数量
     * @return {number} 返回随机后的新数字
     */
    splitInToTwoimensionalArray: function(arr, subArrayNum) {
      var dataArr = new Array(Math.ceil(arr.length / subArrayNum));
      for (let i = 0; i < dataArr.length; i++) {
        dataArr[i] = new Array();
      }
      for (let i = 0; i < arr.length; i++) {
        dataArr[parseInt(i / subArrayNum)][i % subArrayNum] = arr[i];
      }
      return dataArr
    }
  }

  return arrayUtil
}())
